package cn.edu.scau.cmi.pengzhijie.hibenate.dao;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.cfg.Configuration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


import cn.edu.scau.cmi.pengzhijie.hibenate.domain.Student;

public class StudentDao {
	private static final Logger log = LoggerFactory.getLogger(StudentDao.class);
	private static final String Name = "name";
	private Session session;
	
	private void setSession() {
			this.session = new Configuration().configure().buildSessionFactory().openSession();
	}
	
	public void save(Student student) {
		try {
			setSession();
			session.beginTransaction();
			session.save(student);
			session.beginTransaction().commit();
			session.close();
			System.out.println("保存成功");
		} catch (RuntimeException e) {
			// TODO: handle exception
			throw e;
		}
	}
	public void delete(Student student) {

		try {
			setSession();
			session.beginTransaction();
			session.delete(student);
			session.beginTransaction().commit();
			session.close();
			System.out.println("删除成功");
		} catch(RuntimeException e) {
			throw e;
		}
	}
	public void updata(Student student) {
		try {
			setSession();
			session.beginTransaction();
			session.update(student);
			session.beginTransaction().commit();
			session.close();
			System.out.println("修改成功");
		} catch(RuntimeException e) {
			throw e;
		}
	}
	public List listAll() {
		try {
			setSession();
			session.beginTransaction();
			Query query = session.createQuery("from Student");
			List list = query.list();
			session.beginTransaction().commit();
			session.close();
			return list;
		} catch (RuntimeException e) {
			// TODO: handle exception
			throw e;
		}
	}
}
